home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / forms / FORMS / sldraw.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  157 lines

  1. /*
  2.  * sldraw.c
  3.  *
  4.  * Drawing routines for sliders
  5.  *
  6.  * Written by: Mark Overmars
  7.  *
  8.  * Version 2.1 b
  9.  * Date: Nov  4, 1992
  10.  */
  11.  
  12. #include "gl/gl.h"
  13. #include "gl/device.h"
  14. #include "forms.h"
  15.  
  16. static float flinear(float val, float smin, float smax, float gmin, float gmax)
  17. /* performs linear interpolation */
  18. {
  19.   if (smin == smax) return gmax;
  20.   else return gmin + (gmax - gmin) * (val - smin) / (smax - smin);
  21. }
  22.  
  23. /***
  24.   Routine to draw a slider
  25. ***/
  26.  
  27. void fl_drw_slider(
  28.      int boxtype,        /* Boxtype for slider */
  29.     float x, float y, float w, float h, /* Bounding box */
  30.     int col1, int col2,    /* Colors used */
  31.     int sltype,        /* Slider type */
  32.     float size,        /* Size of slider in box [0..1] */
  33.     float val,        /* Value (0=bottom, 1=top) */
  34.         char str[])        /* Label to be drawn in the slider */
  35.  
  36. /* Draws a slider */
  37. {
  38.   float xsl,ysl,hsl,wsl;
  39.   int slbox;
  40.  
  41.   /* Calculate the slider size */
  42.   if (sltype == FL_VERT_SLIDER || sltype == FL_VERT_NICE_SLIDER)
  43.   {
  44.     hsl = size * (h - 2.0*FL_SLIDER_BW1);
  45.     ysl = flinear(val,0.0,1.0,y+FL_SLIDER_BW1,y+h-FL_SLIDER_BW1-hsl);
  46.     wsl = w - 2.0 * FL_SLIDER_BW1;
  47.     xsl = x + FL_SLIDER_BW1;
  48.   }
  49.   else if (sltype == FL_HOR_SLIDER || sltype == FL_HOR_NICE_SLIDER)
  50.   {
  51.     wsl = size * (w - 2.0*FL_SLIDER_BW1);
  52.     xsl = flinear(val,0.0,1.0,x+FL_SLIDER_BW1,x+w-FL_SLIDER_BW1-wsl);
  53.     hsl = h - 2.0 * FL_SLIDER_BW1;
  54.     ysl = y + FL_SLIDER_BW1;
  55.   }
  56.   else if (sltype == FL_VERT_FILL_SLIDER)
  57.   {
  58.     hsl = val*(h-2.0*FL_SLIDER_BW1);
  59.     ysl = y + FL_SLIDER_BW1;
  60.     wsl = w - 2.0 * FL_SLIDER_BW1;
  61.     xsl = x + FL_SLIDER_BW1;
  62.   }
  63.   else if (sltype == FL_HOR_FILL_SLIDER)
  64.   {
  65.     wsl = val*(w-2.0*FL_SLIDER_BW1);
  66.     xsl = x + FL_SLIDER_BW1;
  67.     hsl = h - 2.0 * FL_SLIDER_BW1;
  68.     ysl = y + FL_SLIDER_BW1;
  69.   }
  70.  
  71.   /* Draw the slider */
  72.   fl_drw_box(boxtype,x,y,w,h,col1,FL_SLIDER_BW1);
  73.   if (sltype == FL_VERT_NICE_SLIDER)
  74.   {
  75.     fl_drw_box(FL_FLAT_BOX,x+w/2.0-2.0,y+FL_SLIDER_BW1,
  76.         4.0,h-2.0*FL_SLIDER_BW1,0,0.0);
  77.     fl_drw_box(FL_UP_BOX,xsl,ysl,wsl,hsl,col1,FL_SLIDER_BW1);
  78.     fl_drw_box(FL_DOWN_BOX,xsl+2.0,ysl+hsl/2.0-2.5,
  79.         wsl-5.0,6.0,col2,FL_SLIDER_BW1-2.0);
  80.   }
  81.   else if (sltype == FL_HOR_NICE_SLIDER)
  82.   {
  83.     fl_drw_box(FL_FLAT_BOX,x+FL_SLIDER_BW1,y+h/2.0-2.0,
  84.         w-2.0*FL_SLIDER_BW1,4.0,0,0.0);
  85.     fl_drw_box(FL_UP_BOX,xsl,ysl,wsl,hsl,col1,FL_SLIDER_BW1);
  86.     fl_drw_box(FL_DOWN_BOX,xsl+wsl/2.0-2.5,ysl+2.0,
  87.         5.0,hsl-4.0,col2,FL_SLIDER_BW1-2.0);
  88.   }
  89.   else
  90.   {
  91.     switch (boxtype)
  92.     {
  93.       case FL_UP_BOX:        slbox = FL_UP_BOX; break;
  94.       case FL_DOWN_BOX:        slbox = FL_UP_BOX; break;
  95.       case FL_FRAME_BOX:    slbox = FL_FRAME_BOX; break;
  96.       case FL_ROUNDED_BOX:    slbox = FL_ROUNDED_BOX; break;
  97.       case FL_RFLAT_BOX:    slbox = FL_ROUNDED_BOX; break;
  98.       case FL_RSHADOW_BOX:    slbox = FL_ROUNDED_BOX; break;
  99.       default:            slbox = FL_BORDER_BOX; break;
  100.     }
  101.     fl_drw_box(slbox,xsl,ysl,wsl,hsl,col2,FL_SLIDER_BW2);
  102.   }
  103.   /* Draw the label */
  104.   fl_drw_text(FL_ALIGN_CENTER,xsl,ysl,wsl,hsl,0,FL_SMALL_FONT,
  105.             FL_NORMAL_STYLE,str);
  106. }
  107.  
  108. /***
  109.   Routine to determine the value indicated by the mouse position.
  110.   The routine return the following possible value
  111.     -2  do a page down (mouse click was below sliding part)
  112.     -1  do a line down (mouse click in down button if available (not yet))
  113.      0  simple adapt value (mouse click in sliding part)
  114.      1  line up
  115.      2  page up
  116.   In all cases, in newval the new value is return as if 0 was returned
  117.   (i.e., you can ignore the returned value and simple set the slider
  118.   to the new value if you don't want page down's etc.).
  119. ***/
  120.  
  121. int fl_get_pos_in_slider(
  122.     float x, float y, float w, float h,  /* Bounding box */
  123.     int sltype,            /* Slider type */
  124.     float size,            /* Slider size */
  125.     float xpos, float ypos,        /* Mouse position */
  126.         float oldval,            /* The old value */
  127.     float *newval)            /* The new value */
  128. {
  129.   float v;
  130.   int ret = 0;
  131.   xpos -= x+FL_SLIDER_BW1; ypos -= y+FL_SLIDER_BW1;
  132.   h -= 2.0*FL_SLIDER_BW1; w -= 2.0*FL_SLIDER_BW1;
  133.   if (sltype == FL_VERT_SLIDER || sltype == FL_VERT_NICE_SLIDER)
  134.   {
  135.     v = flinear(ypos,0.5*size*h,h-0.5*size*h,0.,1.);
  136.     if (ypos < oldval * (1.0-size) * h) ret = -2;
  137.     if (ypos > (oldval * (1.0-size) + size) * h) ret = 2;
  138.   }
  139.   else if (sltype == FL_HOR_SLIDER || sltype == FL_HOR_NICE_SLIDER)
  140.   {
  141.     v = flinear(xpos,0.5*size*w,w-0.5*size*w,0.,1.);
  142.     if (xpos < oldval * (1.0-size) * w) ret = -2;
  143.     if (xpos > (oldval * (1.0-size) + size) * w) ret = 2;
  144.   }
  145.   else if (sltype == FL_VERT_FILL_SLIDER)
  146.     { v = flinear(ypos,0.0,h,0.,1.); }
  147.   else if (sltype == FL_HOR_FILL_SLIDER)
  148.     { v = flinear(xpos,0.0,w,0.,1.); }
  149.   if (v < 0.0) v = 0.0;
  150.   if (v > 1.0) v = 1.0;
  151.   if (getbutton(LEFTSHIFTKEY) || getbutton(RIGHTSHIFTKEY))
  152.     *newval = oldval + (v-oldval)*FL_SLIDER_FINE;
  153.   else
  154.     *newval = v;
  155.   return ret;
  156. }
  157.